package com.team.sion.controller;

import com.team.sion.common.SuccessMessage;
import com.team.sion.common.WarningMessage;
import com.team.sion.mapper.SingerMapper;
import com.team.sion.pojo.Consumer;
import com.team.sion.pojo.Singer;
import com.team.sion.pojo.Song;
import com.team.sion.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongService songService;
    @Autowired
    private SingerMapper singerMapper;


    @RequestMapping("")
    public Object SongList() {
        List<Song> list = songService.findAll();
        return new SuccessMessage<List<Song>>("singer", list).getMessage();
    }

    //    name: 12345
//    introduction: 12345
//    lyric: [00:00:00]暂无歌词
//    file: （二进制）
//    singerId: 1
    @RequestMapping("/add")
    public Object addSong(@RequestParam String name,
                          @RequestParam String introduction,
                          @RequestParam String lyric,
                          @RequestParam("file") MultipartFile avatorFile,
                          @RequestParam Integer singerId,
                          HttpServletRequest request) throws Exception {
        Song song = new Song();
        song.setName(name);
        song.setLyric(lyric);
        song.setSingerId(singerId);
        song.setIntroduction(introduction);

        // ⼀.定义⽂件名
        String fileName = "";
        // 1.获取原始⽂件名
        String uploadFileName = avatorFile.getOriginalFilename();
        // 2.截取⽂件扩展名
//        String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
        // 3.把⽂件加上随机数，防⽌⽂件重复
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 4.判断是否输⼊了⽂件名
//        if (!StringUtils.isEmpty(""+id)) {
//            fileName = uuid + "_" + id + "." + extendName;
//        } else {
//            fileName = uuid + "_" + uploadFileName;
//        }
//        System.out.println(fileName);
        fileName = uuid + "_" + uploadFileName;
        // ⼆.获取⽂件路径
        ServletContext context = request.getServletContext();
        String basePath = context.getRealPath("/img/MP3");
        // 三.解决同⼀⽂件夹中⽂件过多问题
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).replaceAll("-", "");
        // 四.判断路径是否存在
        File file = new File(basePath + "/" + datePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String songPath = "img/MP3  " + "/" + datePath + "/" + fileName;
        songService.addSong(song, songPath);
        // 5.使⽤ MulitpartFile 接⼝中⽅法，把上传的⽂件写到指定位置
        avatorFile.transferTo(new File(file, fileName));
        return new SuccessMessage<String>("success", songPath).getMessage();
    }

    @RequestMapping("/detail")
    public Object detail(Integer id) {
        List<Song> list = songService.findById(id);
        return new SuccessMessage<List<Song>>("detail", list).getMessage();
    }

    @RequestMapping("/update")
    public Object updateSong(Song song) {
        boolean flag = songService.updateSong(song);
        if (flag) {
            return new SuccessMessage<>("update").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    @RequestMapping("/img/update")
    public Object updateSongImg(@RequestParam("file") MultipartFile avatorFile,
                                @RequestParam("id") Integer id,
                                HttpServletRequest request) throws Exception {
        // ⼀.定义⽂件名
        String fileName = "";
        // 1.获取原始⽂件名
        String uploadFileName = avatorFile.getOriginalFilename();
        // 2.截取⽂件扩展名
//        String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
        // 3.把⽂件加上随机数，防⽌⽂件重复
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 4.判断是否输⼊了⽂件名
//        if (!StringUtils.isEmpty(""+id)) {
//            fileName = uuid + "_" + id + "." + extendName;
//        } else {
//            fileName = uuid + "_" + uploadFileName;
//        }
//        System.out.println(fileName);
        fileName = uuid + "_" + uploadFileName;
        // ⼆.获取⽂件路径
        ServletContext context = request.getServletContext();
        String basePath = context.getRealPath("/img/songPic");
        // 三.解决同⼀⽂件夹中⽂件过多问题
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).replaceAll("-", "");
        // 四.判断路径是否存在
        File file = new File(basePath + "/" + datePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String songPic = "img/songPic" + "/" + datePath + "/" + fileName;
        songService.updatePic(id, songPic);
        // 5.使⽤ MulitpartFile 接⼝中⽅法，把上传的⽂件写到指定位置
        avatorFile.transferTo(new File(file, fileName));
        return new SuccessMessage<String>("success", songPic).getMessage();
    }

    @RequestMapping("/singer/detail")
    public Object singerDetail(Integer singerId) {
        List<Song> list = songService.findBySingerId(singerId);
        return new SuccessMessage<List<Song>>("detail", list).getMessage();
    }

    @RequestMapping("/singerName/detail")
    public Object singerDetail(String name) {
        List<Song> list = songService.findByName(name);
        return new SuccessMessage<List<Song>>("detail", list).getMessage();
    }

    @RequestMapping("/singerName/getSong")
    public Object songDetail(@RequestParam String singerName,
                             @RequestParam String songName) {
        Integer sid = singerMapper.findByName(singerName);
        List<Song> list = songService.findBySSName(sid, songName);
        return new SuccessMessage<List<Song>>("detail", list).getMessage();
    }
}
